package kibahed.uni.fileinteractions;

import java.io.File;
import java.io.FileOutputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

/**
 * This utility class provides static methods for file access.
 * 
 * @author 
 * 
 */
public final class FileInteractions {


	private FileInteractions() {
		// left empty intentioinally as utility classes should not be
		// instantiated
	}

	
	public static Object loadMarshalledObject(File file, JAXBContext jaxbc) {
		try {
			Unmarshaller u = jaxbc.createUnmarshaller();
			return u.unmarshal(new File(file.getPath()));
		} catch (JAXBException e) {
			e.printStackTrace();
		}

		return null;
	}

	
	public static void saveObjectToFile(Object o, File file, JAXBContext jaxbc) {
		try {
			
			
			Marshaller m = jaxbc.createMarshaller();
			m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
			m.marshal(o,
					new FileOutputStream(file.getPath().replaceAll(".xml", "")
							+ ".xml"));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static File openLoadDialog(final String[] extension,
			int fileSelectionMode) {
		return FileOperations.openFile(extension,
				GlobalDefinitions.fileOpenChooserTitel,
				GlobalDefinitions.fileOpenFileType,
				GlobalDefinitions.fileOpenDialogType,
				GlobalDefinitions.filedefaultPath);
	}

	public static File openSaveDiaolg() {
		return FileOperations.saveFile(
				GlobalDefinitions.fileSaveChooserTitel,
				GlobalDefinitions.fileSaveDialogType,
				GlobalDefinitions.fileSaveFileType,
				GlobalDefinitions.filedefaultPath);
	}
	
	public static File openSaveDiaolg(String path) {
		return FileOperations.saveFile(
				GlobalDefinitions.fileSaveChooserTitel,
				GlobalDefinitions.fileSaveDialogType,
				GlobalDefinitions.fileSaveFileType,
				path);
	}
	
}
